<?php
// +----------------------------------------------------------------------
// | 专利订单管理（下单，后台读取列表、详情等操作）
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2016-1-14 下午5:29:04
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');
class Patent_order_mondel extends CI_Model{
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->table_name = 'patent_order';
	}
	
	/**
	 * 执行wher条件
	 * @param unknown_type $where
	 * @author Baip
	 */
	private function _where($where){
		if ( is_array( $where ) ){
			foreach ( $where as $key => $val ){
				if ( is_array( $val ) ){
					$this->db->where_in($key, $val);
					unset($where[$key]);
				}else{
					$this->db->where($key, $val);
				}
			}
		}
		return true;
	}
	
	/**
	 * 添加数据
	 */
	public function addData($patent_price){
		if ( count($patent_price) > 0  ){
			$data_arr['order_id']      = date('YmdHis');
			$data_arr['patent_type']   = $patent_price['id'];
			$data_arr['officer_type']  = $patent_price['office_id'];
			$data_arr['server_price']  = $patent_price['service_price'];
			$data_arr['officer_price'] = $patent_price['office_price'];
			$data_arr['ontime']        = time();
			$this->db->trans_start();//开启事务
			$this->db->insert($this->table_name, $data_arr);
			$insert_id = $this->db->insert_id();
			//引入专利订单联系人模型(因为CI不能直接引继承另一个模型，只能用下面方法获取另一个数据模型)
			$this->load->model('Patent_order_contacts_model', 'contact');
// 			$ci = &get_instance();
// 			$ci->contact->addData($insert_id);
			$contacts_sql = $this->bindContactsSql($insert_id);
			$this->db->query($contacts_sql);
			$this->db->trans_complete();//结束事务
			if ( $this->db->trans_status() ){
				return $insert_id;
			}else{
				return false;
			}	
		}else{
			return false;
		}	
	}
	
	/**
	 * 拼装订单联系人SQL语句
	 * @param unknown_type $insert_id
	 * @author Baip
	 */
	private function bindContactsSql($insert_id){
		$post_contacts_data = $this->postContactsData();
		$post_contacts_data['pid'] = $insert_id;
		//$tempKey和$tempValue两个数组临时存储插入的数据
		$temp_key_string = "";
		$temp_value_string = "";
		//遍历取出关联数组中的数据并将其存储在字符串中
		foreach($post_contacts_data as $key=>$value)
		{
			$temp_key_string .= "`".$key."`,";
			$temp_value_string .= "'".$value."',";
		}
		//除去末尾的逗号
		$temp_key_string = substr($temp_key_string,0,-1);
		$temp_value_string = substr($temp_value_string,0,-1);
		$sql = "INSERT INTO ".$this->db->dbprefix('patent_order_contacts')."(".$temp_key_string.")  VALUES (".$temp_value_string.")";
		return $sql;
	}
	
	/**
	 * post接收值
	 */
	private function postContactsData(){
		$real_name = $this->input->post('real_name',true);//称呼
		$mobile    = $this->input->post('mobile',true);//手机号
		$email     = $this->input->post('email',true);//邮箱
		$qq        = $this->input->post('qq',true);//qq
		$weixin    = $this->input->post('weixin',true);//微信号
	
		//产生随机字符串
		$data_arr['real_name'] = $real_name;
		$data_arr['mobile'] = $mobile;
		$data_arr['email'] = $email;
		$data_arr['qq'] = $qq;
		$data_arr['weixin'] = $weixin;
		return $data_arr;
	}
}